// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   MessageBox.java

package canvas;

import java.util.Vector;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

// Referenced classes of package mquran:
//            QuranCanvas

public class MessageBox
{

    public MessageBox(String caption, int width, int type, int bar)
    {
        this.type = 0;
        height = 0;
        if(caption != null)
        {
            captions = stringToArray(QuranCanvas.font, caption, width, 0, true);
            height = captions.length * QuranCanvas.font.getHeight() + 4;
        }
        bar_size = bar;
        this.type = type;
        if(bar_size > 0)
            height += 12;
        this.width = width + 4;
    }

    public void draw(Graphics g, int swidth, int sheight)
    {
        int x = (swidth >> 1) - (width >> 1);
        int y = sheight - height - 2;
        g.setClip(x, y, width + 1, height + 1);
        g.setColor(0xffffff);
        g.drawLine(x, y, x + width, y);
        g.drawLine(x, y, x, y + height);
        g.setColor(0x808080);
        g.drawLine(x, y + height, x + width, y + height);
        g.drawLine(x + width, y, x + width, y + height);
        g.setColor(0xe07cc2);
        g.fillRect(x + 1, y + 1, width - 1, height - 1);
        g.setColor(0);
        if(captions != null)
        {
            g.setFont(QuranCanvas.font);
            x = swidth >> 1;
            y += 2;
            for(int i = 0; i < captions.length; i++)
            {
                g.drawString(captions[i], x, y, 17);
                y += QuranCanvas.font_height;
            }

        }
        if(bar_size > 0)
        {
            int w = width - 4;
            x = ((swidth >> 1) - (width >> 1)) + 2;
            g.setColor(0x362f2d);
            g.fillRect(x, y, w, 10);
            g.setColor(0xa39b84);
            w = (w * bar_progress) / bar_size;
            g.fillRect(x, y, w, 4);
            g.setColor(0x867b5e);
            g.fillRect(x, y + 4, w, 2);
            g.setColor(0x56c5f3b);
            g.fillRect(x, y + 6, w, 2);
            g.setColor(0x56471e);
            g.fillRect(x, y + 8, w, 2);
        }
    }

    public static String[] stringToArray(Font font, String s, int width, int max, boolean flag)
    {
        if(font == null || s == null || width <= 0 || s.length() == 0)
            return new String[0];
        max = max > 0 ? max : 0x7fffffff;
        int ai[] = a(font, s);
        if(!flag && ai[0] <= width)
            return (new String[] {
                s
            });
        Vector vector = new Vector();
        int k = s.length();
        int l = s.indexOf(' ');
        if(flag || l >= 0 && vector.size() <= max)
        {
            int i1 = 0;
            int l2;
            for(int k1 = 1; k1 < ai.length; k1 = (i1 = l2) + 1)
            {
                int i2 = 0;
                boolean flag1 = false;
                do
                {
                    if(k1 >= ai.length || i2 + ai[k1] > width)
                        break;
                    if(flag && s.charAt(k1 - 1) == '\n')
                    {
                        flag1 = true;
                        k1++;
                        break;
                    }
                    i2 += ai[k1++];
                } while(true);
                l2 = k1 - 1;
                String s2 = s.substring(i1, flag1 && l2 > i1 ? l2 - 1 : l2);
                for(; l2 < k && s.charAt(l2) == ' '; l2++);
                int i3;
                if(!flag1 && l2 == k1 - 1 && k1 < ai.length && (i3 = s2.lastIndexOf(' ')) >= 0)
                {
                    l2 = i1 + i3;
                    s2 = s.substring(i1, l2);
                    l2++;
                }
                vector.addElement(s2);
            }

        }
        if(!flag && (l < 0 || vector.size() > max))
        {
            int j1 = 0;
            int l1 = 1;
            vector.removeAllElements();
            int k2;
            for(; l1 < ai.length; l1 = (j1 = k2) + 1)
            {
                for(int j2 = 0; l1 < ai.length && j2 + ai[l1] <= width; j2 += ai[l1++]);
                k2 = l1 - 1;
                String s1 = s.substring(j1, k2);
                for(; k2 < k && s.charAt(k2) == ' '; k2++);
                vector.addElement(s1);
            }

        }
        String as[] = new String[vector.size()];
        vector.copyInto(as);
        return as;
    }

    private static int[] a(Font font, String s)
    {
        int len = s.length();
        int ai[] = new int[len + 1];
        int sum = 0;
        for(int k = 1; k <= len; k++)
        {
            char c1 = s.charAt(k - 1);
            sum += ai[k] = font.charWidth(c1);
        }

        ai[0] = sum;
        return ai;
    }

    public static final int OK = 1;
    public static final int CANCEL = 2;
    public static final int OK_CANCEL = 3;
    public String captions[];
    public int bar_size;
    public int bar_progress;
    public int width;
    public int height;
    public int type;
}
